package
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;

	internal function csAddToContainer(container:DisplayObjectContainer, mc:MovieClip, depth:int):void
	{
		mc[_attribute.ATTR_DISPLAY_DEPTH] = depth;
		
		var n:int = container.numChildren;
		for(var i:int = 0; i < n; ++i)
		{
			var child:DisplayObject = container.getChildAt(i);
			
			var childLevel:int = int.MIN_VALUE;
			if(child[_attribute.ATTR_DISPLAY_DEPTH] != null)
			{
				childLevel = child[_attribute.ATTR_DISPLAY_DEPTH];
			}
			
			if(depth < childLevel)
			{
				container.addChildAt(mc, i);
				break;
			}
			
		}
		
		if(!container.contains(mc))
		{
			container.addChild(mc);
		}
		
	}
}